package com.atqh.srrt.chicken.service.impl;

import com.atqh.srrt.chicken.Vo.LoginUserVo;
import com.atqh.srrt.chicken.Vo.UserVo;
import com.atqh.srrt.chicken.domain.SysUser;
import com.atqh.srrt.chicken.domain.UserRegular;
import com.atqh.srrt.chicken.mapper.SysUserMapper;
import com.atqh.srrt.chicken.mapper.UserRegularMapper;
import com.atqh.srrt.chicken.service.LoginService;
import com.atqh.srrt.chicken.service.SysUserService;
import com.atqh.srrt.chicken.utils.ErrorCode;
import com.atqh.srrt.chicken.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
/**
 * @Transactional
 * 事务注解，防止进程终止，仍对数据库进行操作
 */
@Transactional
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserRegularMapper userRegularMapper;

    @Override
    public SysUser findUserById(Long id) {
        /*防止出现id为空的情况*/
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setNickname("码神之路   ");

        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount, account);
        queryWrapper.eq(SysUser::getPassword, password);
        queryWrapper.select(SysUser::getId, SysUser::getAdmin,SysUser::getAccount, SysUser::getAvatar, SysUser::getNickname);
        queryWrapper.last("limit 1");
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserByToken(String token) {

        SysUser sysUser = loginService.checkToken(token);
        log.info("Token{}",token);
        log.info("查询到的redis用户{}",sysUser);
        if (sysUser == null) {
            return Result.Fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        LambdaQueryWrapper<UserRegular> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRegular::getUserId,sysUser.getId()).last("limit 1");
        UserRegular userRegular = userRegularMapper.selectOne(queryWrapper);

        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(sysUser.getId());
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setIsDingzhi(userRegular != null);
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getAccount, account);
        lambdaQueryWrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
        return sysUser;
    }

    @Override
    public void save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo findUserVoById(Long authorId) {

        SysUser sysUser = sysUserMapper.selectById(authorId);
        if(sysUser==null){
            sysUser=new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/touxiang1.png");
            sysUser.setNickname("空");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        //设置userVo的id
        userVo.setId(sysUser.getId());
        return userVo;
    }
}
